package com.hexacta.hrs.services.feedbacks.impl;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.vectrics.employee.Employee;
import org.vectrics.user.Person;

import com.hexacta.hrs.domain.feedback.Feedback;
import com.hexacta.hrs.domain.feedback.FeedbackMotive;
import com.hexacta.hrs.domain.feedback.FeedbackPeriod;
import com.hexacta.hrs.domain.feedback.FeedbackPeriodFactory;
import com.hexacta.hrs.domain.feedback.FeedbackPeriodMentorReport;
import com.hexacta.hrs.domain.feedback.FeedbackPeriodResume;
import com.hexacta.hrs.domain.feedback.FeedbackPeriodResumeByRole;
import com.hexacta.hrs.domain.feedback.Period;
import com.hexacta.hrs.repositories.employee.EmployeeRepository;
import com.hexacta.hrs.repositories.feedback.FeedbackPeriodRepository;
import com.hexacta.hrs.repositories.feedback.PeriodObjectRepository;
import com.hexacta.hrs.services.feedbacks.FeedbackPeriodService;

public class FeedbackPeriodServiceImpl implements FeedbackPeriodService {

    private FeedbackPeriodFactory feedbackPeriodFactory;

    private FeedbackPeriodRepository feedbackPeriodRepository;

    private EmployeeRepository employeeRepository;

    private PeriodObjectRepository periodObjectRepository;

    public FeedbackPeriodFactory getFeedbackPeriodFactory() {
        return feedbackPeriodFactory;
    }

    public void setFeedbackPeriodFactory(final FeedbackPeriodFactory feedbackPeriodFactory) {
        this.feedbackPeriodFactory = feedbackPeriodFactory;
    }

    public FeedbackPeriodRepository getFeedbackPeriodRepository() {
        return feedbackPeriodRepository;
    }

    public void setFeedbackPeriodRepository(final FeedbackPeriodRepository feedbackPeriodRepository) {
        this.feedbackPeriodRepository = feedbackPeriodRepository;
    }

    public PeriodObjectRepository getPeriodObjectRepository() {
        return periodObjectRepository;
    }

    public void setPeriodObjectRepository(final PeriodObjectRepository periodObjectRepository) {
        this.periodObjectRepository = periodObjectRepository;
    }

    @Override
    public List<FeedbackPeriodResume> getAllFeedbackPeriodResumes() {
        List<FeedbackPeriodResume> resultado = getFeedbackPeriodRepository().getFeedbackPeriodResumes();
        return resultado;
    }

    /**
     * @return the employeeRepository
     */
    public EmployeeRepository getEmployeeRepository() {
        return employeeRepository;
    }

    /**
     * @param employeeRepository
     *            the employeeRepository to set
     */
    public void setEmployeeRepository(final EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;
    }

    @Override
    public void createActualFeedbackPeriod() {
        feedbackPeriodFactory.buildAll();
    }

    @Override
    public boolean validateIfCurrentFeedbackPeriodIsAlreadyCreated() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String period = sdf.format(new Date());
        return feedbackPeriodRepository.existFeedbackPeriodForPeriod(period);
    }

    @Override
    public void update(final FeedbackPeriod period) {
        feedbackPeriodRepository.save(period);
    }

    @Override
    public Collection<FeedbackPeriod> matching(final FeedbackPeriod period) {
        return feedbackPeriodRepository.matching(period);
    }

    @Override
    public List<FeedbackPeriod> findByEvaluatedEmployee(final Employee employee) {
        return feedbackPeriodRepository.findByEmployee(employee);
    }

    // @Override
    // public Collection<FeedbackPeriod> findWith(final Employee evaluated,
    // final JobFunction role, final Employee mentor) {
    //
    // return feedbackPeriodRepository.findWith(evaluated, role, mentor);
    // }

    @Override
    public FeedbackPeriod findWithFeedback(final Feedback feedback) {
        return feedbackPeriodRepository.findWithFeedback(feedback);
    }

    @Override
    public void save(final FeedbackPeriod feedbackPeriod) {
        feedbackPeriodRepository.save(feedbackPeriod);

    }

    // public List<FeedbackPeriod> findOpenFeedbacks(final String role) {
    // return feedbackPeriodRepository.findOpenFeedbacks(role);
    // }

    public List<FeedbackPeriod> findOpenFeedbacks(final String role, final Period period) {
        return feedbackPeriodRepository.findOpenFeedbacks(role, period);
    }

    @Override
    public FeedbackPeriod findById(final Long id) {
        return feedbackPeriodRepository.forId(id);
    }

    // @Override
    // public List<FeedbackPeriod> findClosedPeriods(final String role) {
    // return feedbackPeriodRepository.findClosedPeriods(role);
    // }

    public List<FeedbackPeriod> findClosedPeriods(final String role, final Period period) {
        return feedbackPeriodRepository.findClosedPeriods(role, period);
    }

    // public List<FeedbackPeriodRole> getFeedbacksPeriodsRole(final
    // List<JobFunction> listFunction, final String period) {
    // return feedbackPeriodRepository.getFeedbacksPeriodsRole(listFunction,
    // period);
    // }

    public boolean updateClosedFeedbackPeriod(final List<FeedbackPeriod> feedbackPeriodList, final boolean closed) {
        return feedbackPeriodRepository.updateClosedFeedbackPeriod(feedbackPeriodList, closed);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.services.feedbacks.FeedbackPeriodService#
     * findFeedbackPeriodsWithCriteria (java.lang.Long, java.lang.Long,
     * java.lang.String, java.lang.Long)
     */
    @Override
    public Collection<FeedbackPeriod> findFeedbackPeriodsWithCriteria(final Period period, final Long signinPersonId,
            final Long employeeId, final String roleId, final Long mentorId) {

        Employee signinEmployee = employeeRepository.getEmployeeByPersonId(signinPersonId);

        Period usePeriod = period;
        if (period == null) {
            usePeriod = periodObjectRepository.getOpenPeriod();
        }

        return feedbackPeriodRepository.findFeedbackPeriodsWithCriteria(usePeriod, signinEmployee.getFunction()
                .getWeight(), employeeId, roleId, mentorId);

    }

    @Override
    public List<FeedbackPeriodResumeByRole> getAllFeedbackPeriodResumesByRole() {
        String period = periodObjectRepository.getOpenPeriod().getYear();
        return feedbackPeriodRepository.getFeedbackPeriodResumeByRole(period);
    }

    public FeedbackPeriod createFeedbackPeriodManually(final Employee evaluated, final Employee evaluator,
            final String project, final String motive, final FeedbackMotive feedbackMotive, final Period period,
            final Integer monthsInProject) {
        return feedbackPeriodRepository.createFeedbackPeriodManually(evaluated, evaluator, project, motive,
                feedbackMotive, period, monthsInProject);
    }

    public List<FeedbackPeriodResumeByRole> getAllFeedbackPeriodResumesByRole(final String period) {
        return feedbackPeriodRepository.getFeedbackPeriodResumeByRole(period);
    }

    @Override
    public Collection<FeedbackPeriodMentorReport> getMentorsReport(final Person mentor, final String period) {
        return feedbackPeriodRepository.getMentorsReport(mentor, period);
    }

    // /*
    // * (non-Javadoc)
    // *
    // * @see
    // *
    // com.hexacta.hrs.services.feedbacks.FeedbackPeriodService#getOpenPeriod()
    // */
    // @Override
    // public String getOpenPeriod() {
    // return feedbackPeriodRepository.getOpenPeriod();
    // }

    public List<FeedbackPeriod> findAllFeedbackPeriodsForLastPeriod() {

        Period p = periodObjectRepository.getLastPeriod();
        return feedbackPeriodRepository.findAllFeedbackPeriodsForPeriod(p);
    }
}
